'use strict';
app.controller('DashboardCtrl', ['$rootScope', '$scope', '$localStorage', '$http', '$state','$modal', 'commonFactory','PinManageFactory',
function ($rootScope, $scope, $localStorage, $http, $state,$modal, commonFactory,PinManageFactory){
            $scope.countPin = 0;
            $scope.checkFirst = true;
            $scope.WalletPublic = 0;
            $scope.countPH = 0;
            $scope.countGH = 0;
            $scope.riskWallet = 0;
            $scope.profitWallet = 0;
            $scope.checkTime = false;
            $scope.notice = "";
            $scope.popup = function (windowClass, StyleModal, Controller, size) {
                $modal.open({
                    windowClass: windowClass,
                    templateUrl: StyleModal,
                    controller: Controller,
                    size: size,
                    resolve: {
                        messeger: function () {
                            return $scope.messeger;
                        },
                        title: function () {
                            return $scope.title;
                        },
                        params: function () {
                            return $scope.userLogin;
                        }
                    }
                })
         };
        if ($localStorage.user.UserId <= 0) 
        {            
            $state.go('login');
        } else {
            $rootScope.user = $localStorage.user;
            var common = new commonFactory();
            GetPinOfUser($localStorage.user.UserId);
            GetWalletPublic();
            CountPH($localStorage.user.UserId);
            CountGH($localStorage.user.UserId);
            //CheckIsFirstForPin($localStorage.user.UserId);
            GetWallet($localStorage.user.UserId);
            function GetPinOfUser(userId) {
                commonFactory.getPinOfUser(userId).then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.countPin = rsp.data.Result;
                    } else {

                    }
                });
            };
            function CheckIsFirstForPin(userId) {
                PinManageFactory.checkIsFirstForPin(userId).then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.checkFirst = true;
                    } else {
                        GetTimeWaitActivePinForPH(userId);
                    }
                });
            };
            function GetWalletPublic() {
                commonFactory.getWalletPublic().then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.WalletPublic = rsp.data.Result.Balance;
                    } else {

                    }
                });
            };
            function CountPH(UserId) {
                commonFactory.countPH(UserId).then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.countPH = rsp.data.Result;
                    } else {

                    }
                });
            };
            function CountGH(UserId) {
                commonFactory.countGH(UserId).then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.countGH = rsp.data.Result;
                    } else {

                    }
                });
            };
            function GetWallet(userId) {
                commonFactory.getWallet(userId).then(function (rsp) {
                    if (rsp.data.Status) {
                        $scope.riskWallet = rsp.data.risk;
                        $scope.profitWallet = rsp.data.profit;
                    } else {

                    }
                });
            };
            function GetTimeWaitActivePinForPH(UserId) {
                PinManageFactory.getTimeWaitActivePinForPH(UserId).then(function (rsp) {
                    if (rsp.data.Status && rsp.data.flag) {
                        $scope.checkTime = true;
                        $scope.AlertTime = "danger";
                        $scope.notice = rsp.data.Result;
                    } else if (rsp.data.Status == false && rsp.data.flag == true) {
                        $scope.messeger = "Your account has been locked due to not executing PH. To unlock and create PH, you need 4 pin in your account. Are you sure ?";
                        $scope.title = 'Confirm';
                        $modal.open({
                            windowClass: "modal-message modal-warning",
                            templateUrl: "WarningModal",
                            controller: "ModalInstanceCtrl",
                            size: "sm",
                            resolve: {
                                messeger: function () {
                                    return $scope.messeger;
                                },
                                title: function () {
                                    return $scope.title;
                                },
                                params: function () {
                                    return $localStorage.user.UserId;
                                }
                            }
                        })
                    } 
                });

            };
        }

    }
]);
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, $state, messeger, title, params, PinManageFactory) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.hiddenBtn = true;
    $scope.ok = function () {
        PinManageFactory.unlockUser(params).then(function successCallback(resp) {
            if(resp.data.Status)
            {
                $scope.messeger = resp.data.Msg;
                $scope.hiddenBtn = false;
            } else {
                $scope.messeger = resp.data.Msg;
                $scope.hiddenBtn = true;
            }
        }, function errorCallback(resp) {

        });
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});